package com.example.smssender;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

/**
 * 短信格式编辑页面 - 创建或编辑短信模板
 */
public class SmsTemplateEditActivity extends AppCompatActivity {
    
    public static final String EXTRA_TEMPLATE_ID = "template_id";
    
    private TextInputEditText etTitle;
    private TextInputEditText etContent;
    private Button btnSave;
    
    private DataManager dataManager;
    private SmsTemplate template;
    private boolean isEditMode = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_template_edit);
        
        dataManager = DataManager.getInstance(this);
        
        // 初始化视图
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        etTitle = findViewById(R.id.etTitle);
        etContent = findViewById(R.id.etContent);
        btnSave = findViewById(R.id.btnSave);
        
        // 检查是否是编辑模式
        String templateId = getIntent().getStringExtra(EXTRA_TEMPLATE_ID);
        if (templateId != null) {
            isEditMode = true;
            template = dataManager.getSmsTemplateById(templateId);
            if (template != null) {
                etTitle.setText(template.getTitle() != null ? template.getTitle() : "");
                etContent.setText(template.getContent() != null ? template.getContent() : "");
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setTitle("编辑短信格式");
                }
            } else {
                Toast.makeText(this, "短信格式不存在", Toast.LENGTH_SHORT).show();
                finish();
            }
        } else {
            template = new SmsTemplate();
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle("新建短信格式");
            }
        }
        
        // 保存按钮点击事件
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTemplate();
            }
        });
    }
    
    private void saveTemplate() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();
        
        // 验证输入
        if (title.isEmpty()) {
            Toast.makeText(this, "请输入格式名称", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (content.isEmpty()) {
            Toast.makeText(this, "请输入短信内容", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 保存模板
        template.setTitle(title);
        template.setContent(content);
        
        if (isEditMode) {
            dataManager.updateSmsTemplate(template);
            Toast.makeText(this, "短信格式已更新", Toast.LENGTH_SHORT).show();
        } else {
            dataManager.addSmsTemplate(template);
            Toast.makeText(this, "短信格式已添加", Toast.LENGTH_SHORT).show();
        }
        
        finish();
    }
    
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}
